﻿//-----------------------------------------------------------------------
// <copyright file="Module.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace App.Modules
{
    using System;
    using App.Exceptions;
    using App.Modules.Message;
    using TileTool.Message;

    /// <summary>
    /// Base class for all application modules.
    /// </summary>
    public abstract class Module
    {
        #region Properties - Public

        /// <summary>
        /// Gets if the module has been started.
        /// </summary>
        public bool Started
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets or sets the module manager that owns this module.
        /// </summary>
        public ModuleManager Manager
        {
            get;
            set;
        }

        #endregion

        #region Methods - Public

        /// <summary>
        /// Called when the application is starting up.
        /// </summary>
        public virtual void Startup()
        {
            this.Started = true;
        }

        /// <summary>
        /// Called when the application is shutting down.
        /// </summary>
        public virtual void Shutdown()
        {
            this.Started = false;
        }

        /// <summary>
        /// Register for any messages the module is interested in.
        /// 
        /// Called after all modules have had Startup() called on them.
        /// </summary>
        public virtual void RegisterForMessages()
        {
        }

        /// <summary>
        /// Unregister for any messages the module is interested in.
        /// 
        /// Called before all modules have had Shutdown() called on them.
        /// </summary>
        public virtual void UnregisterForMessages()
        {
        }

        /// <summary>
        /// Called each time the application goes idle.
        /// </summary>
        public virtual void Work()
        {
        }

        #endregion

        #region Methods - Protected

        /// <summary>
        /// Given a type, find the module that is of that type in the module
        /// manager.
        /// </summary>
        /// <param name="failIfNotFound">If true, will throw an exception if a module is not found.</param>
        /// <typeparam name="T">Module type to find.</typeparam>
        /// <returns>Returns the module if it is found or null if it is not.</returns>
        protected T GetModule<T>(bool failIfNotFound = true) where T : Module
        {
            if (this.Manager == null)
            {
                throw new InvalidOperationException("Manager must be non-null to make this call.");
            }

            T module = this.Manager.GetModule<T>();
            if (failIfNotFound && module == null)
            {
                throw new AppException("Unable to find module: " + typeof(T).Name);
            }

            return module;
        }

        /// <summary>
        /// Send the passed message using the message module.
        /// 
        /// Since sending a message is something very common, this was
        /// added to the base module class so that all modules can use it.
        /// </summary>
        /// <param name="msg">Message to send.</param>
        protected void SendMessage(MessageBase msg)
        {
            MessageModule messageModule = this.GetModule<MessageModule>();

            messageModule.MessageRouter.SendMessage(msg);
        }

        #endregion
    }
}
